package com.mmlc.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mmlc.meimei.lichengkongjian.R;


/**
 * Created by 何强 on 2016/7/18 0018.
 */
public class TitleBarView extends RelativeLayout implements View.OnClickListener{

    private ImageView btnLeft;     //左侧按钮
    private TextView btnRight;    //右侧按钮
    private TextView tv_center;   //标题文本
    public LinearLayout ll_left;
    public RelativeLayout ll_right;
    private BtnClickListener listener;

    public TitleBarView(Context context) {
        super(context);
    }

    public TitleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    /**
     * 初始化组件
     * @param context
     */
    private void init(Context context){
        LayoutInflater.from(context).inflate(R.layout.cusview_titlebar,this);
        btnLeft = (ImageView) findViewById(R.id.btn_titlebar_left);
        btnRight = (TextView) findViewById(R.id.btn_titlebar_right);
        tv_center = (TextView) findViewById(R.id.tv_center);
        ll_left = (LinearLayout) findViewById(R.id.ll_left);
        ll_right = (RelativeLayout) findViewById(R.id.ll_right);

        //默认让右边的标题栏隐藏
        hideRight();
    }

    private void hideRight() {
        ll_right.setVisibility(View.INVISIBLE);
    }

    public void setTitleBarListener(BtnClickListener listener){
        this.listener = listener;
    }

    /**
     * 按钮点击接口
     */
    public interface BtnClickListener{
        void leftClick();
        void rightClick();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_titlebar_left:
                listener.leftClick();
                break;
            case R.id.btn_titlebar_right:
                listener.rightClick();
                break;
            default:
                break;
        }
    }

    /**
     * 设置左侧按钮是否可见
     * @param flag  是否可见
     */
    public void setLeftBtnVisable(boolean flag){
        if (flag){
            btnLeft.setVisibility(VISIBLE);
        }
        else {
            btnLeft.setVisibility(GONE);
        }
    }
    public ImageView getLeftBtn(){
        return btnLeft;
    }
    /**
     * 设置右侧按钮是否可见
     * @param flag  是否可见
     */
    public void setRightBtnVisable(boolean flag){
        if (flag){
            ll_right.setVisibility(VISIBLE);
        }
        else {
            ll_right.setVisibility(GONE);
        }
    }
    public void setRightText(String text){
            btnRight.setText(text);
    }
    public String getRightText(){
        return btnRight.getText().toString();
    }
    public RelativeLayout getRightBtn(){
        return  ll_right;
    }
    public void setCenterName(String name){
        tv_center.setText(name);
    }
}